<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="开课学年">
              <a-select allowClear v-model="queryParam.xn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="开课学期">
              <a-select allowClear v-model="queryParam.xqm">
                <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="6">
            <a-form-item label="占用周数">
              <a-input v-model="queryParam.zyzZs">
                <div slot="addonBefore">第</div>
                <div slot="addonAfter">周</div>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :md="3" :sm="6">
            <a-form-item>
              <a-input v-model="queryParam.week">
                <div slot="addonBefore">周</div>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="占用院系">
              <a-select :disabled="this.selectMenu.yxList.length==1" allowClear v-model="queryParam.yx">
                <a-select-option v-for="i in this.selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh +'-'+i.dwmc
                  }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="教室名称">
              <a-input v-model="queryParam.classroomNo"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="8" :sm="12">
            <a-form-item label="所在校区">
              <a-select allowClear v-model="queryParam.jsxq">
              <a-select-option v-for="i in selectMenu.campusList" :key="i.id" :value="i.xqbh">{{ i.xqmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <template v-if="advanced">

            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="所属院系">
                <a-select allowClear :disabled="selectMenu.yxList.length === 1"  v-model="queryParam.deptCode" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="是否可用">
                <a-select allowClear v-model="queryParam.isUsable">
                  <a-select-option value="1">可用</a-select-option>
                  <a-select-option value="0">不可用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="12">
              <a-form-item label="教室类型">
                <a-select allowClear v-model="queryParam.typeCode">
                  <a-select-option v-for="i in TYPE0046" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

          </template>
          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="toAdd" style="margin:0 10px">添加教室占用</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()" style="margin-left: 8px;">重置</a-button>

            <!-- <a-button type="primary" @click="batchDelete" style="margin:0 10px">删除</a-button> -->
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: true}"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x: 1200}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>

        <span slot="zysj" slot-scope="text,record">
          第{{ record.zyzZs }}周 周{{record.week}}
        </span>
        <span slot="jsmc" slot-scope="text,record">
          <a-tooltip placement="top" :title="record.classrooms.map(_=>_.classroomNo).join('、')">
            {{record.classrooms.map(_=>_.classroomNo).join('、')}}
          </a-tooltip>
        </span>
        <span slot="xq" slot-scope="text,record">
          {{record.classrooms[0].jsxqmc}}
        </span>
        <span slot="jslx" slot-scope="text,record">
          {{record.classrooms[0].typeCodeName}}
        </span>
        <span slot="isUsable" slot-scope="text">
          {{ +text === 1 ? '可用' : '不可用' }}
        </span>
        <a slot="classroom" slot-scope="a,b" @click="showDetail(b.id)">{{a}}</a>
        <template slot="operation" slot-scope="id">
          <a @click="delectRoom(id)">取消</a>
          <a @click="roomInfo(id)">详情</a>
        </template>
      </s-table>
    </a-card>

    <add-modal ref="info" :editId="editId" :visible="addVisible" @cancel="addVisible = false" @refresh="$refs.table.refresh(false)"/>
    <occupancy-info ref="roomInfo" :infoVisible="infoVisible" @infocancel="infoVisible = false"></occupancy-info>
  </div>
</template>

<script>
import { STable } from '~'
import addModal from './addModal'
import occupancyInfo from './occupancyInfo'
import searchMixin from '@/utils/searchRemain'
import { mapState } from 'vuex'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: {customRender: 'serial'}
        },
        {
          title: '学年',
          dataIndex: 'xn',
        },

        {
          title: '学期',
          dataIndex: 'xqmc',
        },
        {
          title: '占用时间',
          dataIndex: 'week',
          scopedSlots: {customRender: 'zysj'}
        },
        {
          title: '开始节课',
          dataIndex: 'kskj',
        },
        {
          title: '结束课节',
          dataIndex: 'jskj'
        },
        {
          title: '占用院系',
          dataIndex: 'yxmc',
        },
        {
          title: '校区',
          dataIndex: 'jsxqmc',
          scopedSlots: {customRender: 'xq'}
        },
        {
          title: '教室类型',
          dataIndex: 'jslxmc',
          scopedSlots: {customRender: 'jslx'}
        },
        {
          title: '教室名称',
          dataIndex: 'jsmc',
          width:250,
          scopedSlots: { customRender: 'jsmc' },
        },
        // {
        //   title: '占用状态',
        //   dataIndex: 'zyztmc'
        // },
        {
          title: '操作',
          dataIndex: 'id',
          scopedSlots: { customRender: 'operation' },
          width:120,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.zyClassroomList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList: []
      },
      addVisible:false,
      infoVisible: false,
      confirmLoading: false,
      editId: null,
      initDone: false
    }
  },
  computed: {
    ...mapState({
      TYPE0046: state => state.app['TYPE0046'],
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0072: state => state.app['TYPE0072'],
    })
  },
  components: {STable, addModal,occupancyInfo},
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0046')
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    // this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.base.campusAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data,
          campusList: res[1].data
        }

        if (this.selectMenu.yxList && this.selectMenu.yxList.length == 1) {
          this.$set(this.queryParam, 'yx', this.selectMenu.yxList[0].dwh)
        }
        this.$set(this.queryParam, 'xn', res[2].schoolYear)
        this.$set(this.queryParam, 'xqm', res[2].currentSemester)
        this.initDone = true
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // batchDelete () {
    //   if (!this.selectedRowKeys.length) {
    //     this.$message.warning('请选择需要删除的教室')
    //     return
    //   }
    //   this.$confirm({
    //     title: '确定删除所选中的教室?',
    //     content: '当您点击确定按钮后，这些记录将会被删除',
    //     centered: true,
    //     onOk: () => {
    //       let ids = this.selectedRowKeys.join(',')
    //       this.$api.cultivate.classroomDelete(ids).then(() => {
    //         this.$message.success('删除成功')
    //         this.selectedRowKeys = []
    //         this.$refs.table.refresh(false)
    //         this.$refs.table.clearSelected()
    //       })
    //     },
    //     onCancel: () => {
    //     }
    //   })
    // },
    delectRoom(id){
          this.$api.cultivate.zyClassroomDelete(id).then(() => {
            this.$message.success('取消占用成功')
            this.$refs.table.refresh(false)
            // this.$refs.table.clearSelected()
          })
    },
    roomInfo(id){
      this.$api.cultivate.zyClassroomInfo(id).then((res) => {
          console.log(res)
          this.infoVisible = true
          this.$refs.roomInfo.info = res.data
      })
    },
    showEdit (id) {
      this.$api.cultivate.classroomInfo(id).then(res => {
        this.$refs.info.fillForm(res.data)
      })
      this.addVisible = true
      this.$refs.info.showOnly = false
      this.editId = id
    },
    showDetail (id) {
      this.$api.cultivate.classroomInfo(id).then(res => {
        this.$refs.info.fillForm(res.data)
        this.$refs.info.showOnly = true
      })
      this.addVisible = true
      this.editId = id
    },
    toAdd () {
      this.$refs.info.initSelectMenu()
      this.addVisible = true
      this.editId = null
      this.$refs.info.form.resetFields()
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    }
  }
}
</script>

<style>

</style>
